<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags"%>

<%@ page
	import="com.onlineMIS.ORM.entity.headQ.user.*,java.util.Date,java.text.SimpleDateFormat"%>
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询采购单据</title>
<%@ include file="../../common/Style.jsp"%>

<script>
var treeGrid;

$(document).ready(function() {
	parent.$.messager.progress('close');
	var params = $('#preGenReportForm').serialize();
	$('#dataGridSup').treegrid({
	url : 'headQReportJSON!getPurchaseStatisticReptEles?'+params,
	idField : 'id',
	queryParams : '',
	treeField : 'name',
	rownumbers : true,
	autoRowHeight : true,
	lines : true,										
	onLoadSuccess : function(row, param) {
		$.messager.progress('close');
	},										
	onBeforeExpand : function(node) {
		$("#parentId").val(
				node.parentId);
		$("#yearId").val(node.yearId);
		$("#quarterId").val(
				node.quarterId);
		$("#brandId").val(node.brandId);
		params = $(
				'#preGenReportForm')
				.serialize();
		$('#dataGridSup').treegrid(
				'options').url = 'headQReportJSON!getPurchaseStatisticReptEles?'
				+ params;
	},										
	frozenColumns : [ [ {
		field : 'name',
		width : 280,
		title : '采购项目列表'
	} ] ],
	columns : [ [
			{
				field : 'purchaseQuantity',
				width : 80,
				title : '采购数量',
				formatter : function(
						value, row,
						index) {
					if (row.purchaseQuantity == 0)
						return "-";
					else
						return row.purchaseQuantity;

				}
			},
			{
				field : 'returnQuantity',
				width : 80,
				title : '退货数量',
				formatter : function(
						value, row,
						index) {
					if (row.returnQuantity == 0)
						return "-";
					else
						return row.returnQuantity;
				}
			},{
				field : 'freeQuantity',
				width : 80,
				title : '获赠数量',
				formatter : function(
						value, row,
						index) {
					if (row.freeQuantity == 0)
						return "-";
					else
						return row.freeQuantity;
				}
			},
			{
				field : 'netQuantity',
				width : 100,
				title : '净采购量'
			},

			{
				field : 'avgPrice',
				width : 100,
				title : '采购单价',
				formatter : function(
						value, row,
						index) {
					return (row.avgPrice)
							.toFixed(2);

				}
			},
			{
				field : 'purchaseAmount',
				width : 100,
				title : '采购金额',
				formatter : function(
						value, row,
						index) {
					return (row.purchaseAmount)
							.toFixed(2);

				}
			},
			{
				field : 'purchaseAmountR',
				width : 100,
				title : '退货金额',
				formatter : function(
						value, row,
						index) {
					return (row.purchaseAmountR)
							.toFixed(2);

				}
			},{
				field : 'purchaseAmountF',
				width : 100,
				title : '获赠金额',
				formatter : function(
						value, row,
						index) {
					return (row.purchaseAmountF)
							.toFixed(2);

				}
			},
			{
				field : 'purchaseTotalAmt',
				width : 100,
				title : '净采购金额',
				formatter : function(
						value, row,
						index) {
					return (row.purchaseTotalAmt)
							.toFixed(2);

				}
			} ] ],
	toolbar : '#toolbarSup',										
	});
});

	function genReport() {
		$("#parentId").val(0);
		$("#yearId").val(0);
		$("#quarterId").val(0);
		$("#brandId").val(0);
		var params = $('#preGenReportForm').serialize();
		$('#dataGridSup').treegrid('options').url = 'headQReportJSON!getPurchaseStatisticReptEles?'
				+ params;
		$('#dataGridSup').treegrid('reload');
	}
	
	
	function exportFile(isDetails) {

		var node = $('#dataGridSup').treegrid('getSelected');

		if (node == null) {
			$.messager.alert('错误', '请先选中一行再继续操作', 'error');
		} else {

			$("#yearId").val(node.yearId);
			$("#quarterId").val(node.quarterId);
			$("#brandId").val(node.brandId);
			if(isDetails){
				document.preGenReportForm.action = "headqReportJSP!downloadPurchaseExcelReportDetails";
			}else{
				document.preGenReportForm.action = "headqReportJSP!downloadPurchaseExcelReport";
			}			
			document.preGenReportForm.submit();
		}
	}
	
	
function downloadLevelExport(){
	var node = $('#dataGridSup').treegrid('getSelected');

	if (node == null) {
		$.messager.alert('错误', '请先选中一行再继续操作', 'error');
	} else {

		$("#yearId").val(node.yearId);
		$("#quarterId").val(node.quarterId);
		$("#brandId").val(node.brandId);
		$("#parentId").val(1);

		document.preGenReportForm.action = "headqReportJSP!downloadPurchaseLevelReport";					
		document.preGenReportForm.submit();
	}
}	
function back(){
    document.preGenReportForm.action="headqReportJSP!preGeneratePurchaseReport";
    document.preGenReportForm.submit();
}
</script>

</head>
<body>
	<div class="easyui-layout" data-options="fit : true,border : false">

		<div data-options="region:'center',border:false">
		            <s:form id="preGenReportForm" name="preGenReportForm"
				action="/action/inventoryOrder!search" theme="simple" method="POST">
				<s:hidden name="formBean.parentId" id="parentId" />
				<input type="hidden" name="formBean.year.year_ID" id="yearId"
					value="0" />
				<input type="hidden" name="formBean.quarter.quarter_ID"
					id="quarterId" value="0" />
				<input type="hidden" name="formBean.brand.brand_ID" id="brandId"
					value="0" />
				<s:hidden name="formBean.startDate" id="startDate" />
				<s:hidden name="formBean.endDate" id="endDate" />
				<s:hidden name="formBean.order.supplier.id" id="supplierId" />
				<s:hidden name="formBean.belongTo" id="belongTo" />
			</s:form>
			<table id="dataGridSup" border="0">
			</table>
			<div id="toolbarSup" style="display: none;">
				<a onclick="back();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-back'">退回上页</a>			
				<a onclick="exportFile(false);" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-print'">导出报表</a>
				<a onclick="exportFile(true);" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-print'">导出明细报表</a>
				<a onclick="downloadLevelExport();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-print'">导出层级报表</a>
			</div>
		</div>
	</div>

</body>
</html>